home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tk4.0 / tkScale.c < prev    next >
C/C++ Source or Header  |  1995-06-09  |  64KB  |  2,053 lines

  1. /* 
  2.  * tkScale.c --
  3.  *
  4.  *    This module implements a scale widgets for the Tk toolkit.
  5.  *    A scale displays a slider that can be adjusted to change a
  6.  *    value;  it also displays numeric labels and a textual label,
  7.  *    if desired.
  8.  *    
  9.  *    The modifications to use floating-point values are based on
  10.  *    an implementation by Paul Mackerras.  The -variable option
  11.  *    is due to Henning Schulzrinne.  All of these are used with
  12.  *    permission.
  13.  *
  14.  * Copyright (c) 1990-1994 The Regents of the University of California.
  15.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  16.  *
  17.  * See the file "license.terms" for information on usage and redistribution
  18.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  19.  */
  20.  
  21. static char sccsid[] = "@(#) tkScale.c 1.72 95/06/09 08:20:04";
  22.  
  23. #include "tkPort.h"
  24. #include "default.h"
  25. #include "tkInt.h"
  26. #include <math.h>
  27.  
  28. /*
  29.  * A data structure of the following type is kept for each scale
  30.  * widget managed by this file:
  31.  */
  32.  
  33. typedef struct {
  34.     Tk_Window tkwin;        /* Window that embodies the scale.  NULL
  35.                  * means that the window has been destroyed
  36.                  * but the data structures haven't yet been
  37.                  * cleaned up.*/
  38.     Display *display;        /* Display containing widget.  Used, among
  39.                  * other things, so that resources can be
  40.                  * freed even after tkwin has gone away. */
  41.     Tcl_Interp *interp;        /* Interpreter associated with scale. */
  42.     Tcl_Command widgetCmd;    /* Token for scale's widget command. */
  43.     Tk_Uid orientUid;        /* Orientation for window ("vertical" or
  44.                  * "horizontal"). */
  45.     int vertical;        /* Non-zero means vertical orientation,
  46.                  * zero means horizontal. */
  47.     int width;            /* Desired narrow dimension of scale,
  48.                  * in pixels. */
  49.     int length;            /* Desired long dimension of scale,
  50.                  * in pixels. */
  51.     double value;        /* Current value of scale. */
  52.     char *varName;        /* Name of variable (malloc'ed) or NULL.
  53.                  * If non-NULL, scale's value tracks
  54.                  * the contents of this variable and
  55.                  * vice versa. */
  56.     double fromValue;        /* Value corresponding to left or top of
  57.                  * scale. */
  58.     double toValue;        /* Value corresponding to right or bottom
  59.                  * of scale. */
  60.     double tickInterval;    /* Distance between tick marks;  0 means
  61.                  * don't display any tick marks. */
  62.     double resolution;        /* If > 0, all values are rounded to an
  63.                  * even multiple of this value. */
  64.     int digits;            /* Number of significant digits to print
  65.                  * in values.  0 means we get to choose the
  66.                  * number based on resolution and/or the
  67.                  * range of the scale. */
  68.     char format[10];        /* Sprintf conversion specifier computed from
  69.                  * digits and other information. */
  70.     double bigIncrement;    /* Amount to use for large increments to
  71.                  * scale value.  (0 means we pick a value). */
  72.     char *command;        /* Command prefix to use when invoking Tcl
  73.                  * commands because the scale value changed.
  74.                  * NULL means don't invoke commands.
  75.                  * Malloc'ed. */
  76.     int repeatDelay;        /* How long to wait before auto-repeating
  77.                  * on scrolling actions (in ms). */
  78.     int repeatInterval;        /* Interval between autorepeats (in ms). */
  79.     char *label;        /* Label to display above or to right of
  80.                  * scale;  NULL means don't display a
  81.                  * label.  Malloc'ed. */
  82.     int labelLength;        /* Number of non-NULL chars. in label. */
  83.     Tk_Uid state;        /* Normal or disabled.  Value cannot be
  84.                  * changed when scale is disabled. */
  85.  
  86.     /*
  87.      * Information used when displaying widget:
  88.      */
  89.  
  90.     int borderWidth;        /* Width of 3-D border around window. */
  91.     Tk_3DBorder bgBorder;    /* Used for drawing slider and other
  92.                  * background areas. */
  93.     Tk_3DBorder activeBorder;    /* For drawing the slider when active. */
  94.     XColor *troughColorPtr;    /* Color for drawing trough. */
  95.     GC troughGC;        /* For drawing trough. */
  96.     GC copyGC;            /* Used for copying from pixmap onto screen. */
  97.     XFontStruct *fontPtr;    /* Information about text font, or NULL. */
  98.     XColor *textColorPtr;    /* Color for drawing text. */
  99.     GC textGC;            /* GC for drawing text in normal mode. */
  100.     int relief;            /* Indicates whether window as a whole is
  101.                  * raised, sunken, or flat. */
  102.     int highlightWidth;        /* Width in pixels of highlight to draw
  103.                  * around widget when it has the focus.
  104.                  * <= 0 means don't draw a highlight. */
  105.     XColor *highlightBgColorPtr;
  106.                 /* Color for drawing traversal highlight
  107.                  * area when highlight is off. */
  108.     XColor *highlightColorPtr;    /* Color for drawing traversal highlight. */
  109.     int inset;            /* Total width of all borders, including
  110.                  * traversal highlight and 3-D border.
  111.                  * Indicates how much interior stuff must
  112.                  * be offset from outside edges to leave
  113.                  * room for borders. */
  114.     int sliderLength;        /* Length of slider, measured in pixels along
  115.                  * long dimension of scale. */
  116.     int showValue;        /* Non-zero means to display the scale value
  117.                  * below or to the left of the slider;  zero
  118.                  * means don't display the value. */
  119.  
  120.     /*
  121.      * Layout information for horizontal scales, assuming that window
  122.      * gets the size it requested:
  123.      */
  124.  
  125.     int horizLabelY;        /* Y-coord at which to draw label. */
  126.     int horizValueY;        /* Y-coord at which to draw value text. */
  127.     int horizTroughY;        /* Y-coord of top of slider trough. */
  128.     int horizTickY;        /* Y-coord at which to draw tick text. */
  129.     /*
  130.      * Layout information for vertical scales, assuming that window
  131.      * gets the size it requested:
  132.      */
  133.  
  134.     int vertTickRightX;        /* X-location of right side of tick-marks. */
  135.     int vertValueRightX;    /* X-location of right side of value string. */
  136.     int vertTroughX;        /* X-location of scale's slider trough. */
  137.     int vertLabelX;        /* X-location of origin of label. */
  138.  
  139.     /*
  140.      * Miscellaneous information:
  141.      */
  142.  
  143.     Cursor cursor;        /* Current cursor for window, or None. */
  144.     char *takeFocus;        /* Value of -takefocus option;  not used in
  145.                  * the C code, but used by keyboard traversal
  146.                  * scripts.  Malloc'ed, but may be NULL. */
  147.     int flags;            /* Various flags;  see below for
  148.                  * definitions. */
  149. } Scale;
  150.  
  151. /*
  152.  * Flag bits for scales:
  153.  *
  154.  * REDRAW_SLIDER -        1 means slider (and numerical readout) need
  155.  *                to be redrawn.
  156.  * REDRAW_OTHER -        1 means other stuff besides slider and value
  157.  *                need to be redrawn.
  158.  * REDRAW_ALL -            1 means the entire widget needs to be redrawn.
  159.  * ACTIVE -            1 means the widget is active (the mouse is
  160.  *                in its window).
  161.  * BUTTON_PRESSED -        1 means a button press is in progress, so
  162.  *                slider should appear depressed and should be
  163.  *                draggable.
  164.  * INVOKE_COMMAND -        1 means the scale's command needs to be
  165.  *                invoked during the next redisplay (the
  166.  *                value of the scale has changed since the
  167.  *                last time the command was invoked).
  168.  * SETTING_VAR -        1 means that the associated variable is
  169.  *                being set by us, so there's no need for
  170.  *                ScaleVarProc to do anything.
  171.  * NEVER_SET -            1 means that the scale's value has never
  172.  *                been set before (so must invoke -command and
  173.  *                set associated variable even if the value
  174.  *                doesn't appear to have changed).
  175.  * GOT_FOCUS -            1 means that the focus is currently in
  176.  *                this widget.
  177.  */
  178.  
  179. #define REDRAW_SLIDER        1
  180. #define REDRAW_OTHER        2
  181. #define REDRAW_ALL        3
  182. #define ACTIVE            4
  183. #define BUTTON_PRESSED        8
  184. #define INVOKE_COMMAND        0x10
  185. #define SETTING_VAR        0x20
  186. #define NEVER_SET        0x40
  187. #define GOT_FOCUS        0x80
  188.  
  189. /*
  190.  * Symbolic values for the active parts of a slider.  These are
  191.  * the values that may be returned by the ScaleElement procedure.
  192.  */
  193.  
  194. #define OTHER        0
  195. #define TROUGH1        1
  196. #define SLIDER        2
  197. #define TROUGH2        3
  198.  
  199. /*
  200.  * Space to leave between scale area and text, and between text and
  201.  * edge of window.
  202.  */
  203.  
  204. #define SPACING 2
  205.  
  206. /*
  207.  * How many characters of space to provide when formatting the
  208.  * scale's value:
  209.  */
  210.  
  211. #define PRINT_CHARS 150
  212.  
  213. /*
  214.  * Information used for argv parsing.
  215.  */
  216.  
  217. static Tk_ConfigSpec configSpecs[] = {
  218.     {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
  219.     DEF_SCALE_ACTIVE_BG_COLOR, Tk_Offset(Scale, activeBorder),
  220.     TK_CONFIG_COLOR_ONLY},
  221.     {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
  222.     DEF_SCALE_ACTIVE_BG_MONO, Tk_Offset(Scale, activeBorder),
  223.     TK_CONFIG_MONO_ONLY},
  224.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  225.     DEF_SCALE_BG_COLOR, Tk_Offset(Scale, bgBorder),
  226.     TK_CONFIG_COLOR_ONLY},
  227.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  228.     DEF_SCALE_BG_MONO, Tk_Offset(Scale, bgBorder),
  229.     TK_CONFIG_MONO_ONLY},
  230.     {TK_CONFIG_DOUBLE, "-bigincrement", "bigIncrement", "BigIncrement",
  231.     DEF_SCALE_BIG_INCREMENT, Tk_Offset(Scale, bigIncrement), 0},
  232.     {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
  233.     (char *) NULL, 0, 0},
  234.     {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
  235.     (char *) NULL, 0, 0},
  236.     {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
  237.     DEF_SCALE_BORDER_WIDTH, Tk_Offset(Scale, borderWidth), 0},
  238.     {TK_CONFIG_STRING, "-command", "command", "Command",
  239.     DEF_SCALE_COMMAND, Tk_Offset(Scale, command), TK_CONFIG_NULL_OK},
  240.     {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
  241.     DEF_SCALE_CURSOR, Tk_Offset(Scale, cursor), TK_CONFIG_NULL_OK},
  242.     {TK_CONFIG_INT, "-digits", "digits", "Digits",
  243.     DEF_SCALE_DIGITS, Tk_Offset(Scale, digits), 0},
  244.     {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
  245.     (char *) NULL, 0, 0},
  246.     {TK_CONFIG_FONT, "-font", "font", "Font",
  247.     DEF_SCALE_FONT, Tk_Offset(Scale, fontPtr),
  248.     0},
  249.     {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
  250.     DEF_SCALE_FG_COLOR, Tk_Offset(Scale, textColorPtr),
  251.     TK_CONFIG_COLOR_ONLY},
  252.     {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
  253.     DEF_SCALE_FG_MONO, Tk_Offset(Scale, textColorPtr),
  254.     TK_CONFIG_MONO_ONLY},
  255.     {TK_CONFIG_DOUBLE, "-from", "from", "From",
  256.     DEF_SCALE_FROM, Tk_Offset(Scale, fromValue), 0},
  257.     {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
  258.     "HighlightBackground", DEF_SCALE_HIGHLIGHT_BG,
  259.     Tk_Offset(Scale, highlightBgColorPtr), 0},
  260.     {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
  261.     DEF_SCALE_HIGHLIGHT, Tk_Offset(Scale, highlightColorPtr), 0},
  262.     {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
  263.     "HighlightThickness",
  264.     DEF_SCALE_HIGHLIGHT_WIDTH, Tk_Offset(Scale, highlightWidth), 0},
  265.     {TK_CONFIG_STRING, "-label", "label", "Label",
  266.     DEF_SCALE_LABEL, Tk_Offset(Scale, label), TK_CONFIG_NULL_OK},
  267.     {TK_CONFIG_PIXELS, "-length", "length", "Length",
  268.     DEF_SCALE_LENGTH, Tk_Offset(Scale, length), 0},
  269.     {TK_CONFIG_UID, "-orient", "orient", "Orient",
  270.     DEF_SCALE_ORIENT, Tk_Offset(Scale, orientUid), 0},
  271.     {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
  272.     DEF_SCALE_RELIEF, Tk_Offset(Scale, relief), 0},
  273.     {TK_CONFIG_INT, "-repeatdelay", "repeatDelay", "RepeatDelay",
  274.     DEF_SCALE_REPEAT_DELAY, Tk_Offset(Scale, repeatDelay), 0},
  275.     {TK_CONFIG_INT, "-repeatinterval", "repeatInterval", "RepeatInterval",
  276.     DEF_SCALE_REPEAT_INTERVAL, Tk_Offset(Scale, repeatInterval), 0},
  277.     {TK_CONFIG_DOUBLE, "-resolution", "resolution", "Resolution",
  278.     DEF_SCALE_RESOLUTION, Tk_Offset(Scale, resolution), 0},
  279.     {TK_CONFIG_BOOLEAN, "-showvalue", "showValue", "ShowValue",
  280.     DEF_SCALE_SHOW_VALUE, Tk_Offset(Scale, showValue), 0},
  281.     {TK_CONFIG_PIXELS, "-sliderlength", "sliderLength", "SliderLength",
  282.     DEF_SCALE_SLIDER_LENGTH, Tk_Offset(Scale, sliderLength), 0},
  283.     {TK_CONFIG_UID, "-state", "state", "State",
  284.     DEF_SCALE_STATE, Tk_Offset(Scale, state), 0},
  285.     {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
  286.     DEF_SCALE_TAKE_FOCUS, Tk_Offset(Scale, takeFocus),
  287.     TK_CONFIG_NULL_OK},
  288.     {TK_CONFIG_DOUBLE, "-tickinterval", "tickInterval", "TickInterval",
  289.     DEF_SCALE_TICK_INTERVAL, Tk_Offset(Scale, tickInterval), 0},
  290.     {TK_CONFIG_DOUBLE, "-to", "to", "To",
  291.     DEF_SCALE_TO, Tk_Offset(Scale, toValue), 0},
  292.     {TK_CONFIG_COLOR, "-troughcolor", "troughColor", "Background",
  293.     DEF_SCALE_TROUGH_COLOR, Tk_Offset(Scale, troughColorPtr),
  294.     TK_CONFIG_COLOR_ONLY},
  295.     {TK_CONFIG_COLOR, "-troughcolor", "troughColor", "Background",
  296.     DEF_SCALE_TROUGH_MONO, Tk_Offset(Scale, troughColorPtr),
  297.     TK_CONFIG_MONO_ONLY},
  298.     {TK_CONFIG_STRING, "-variable", "variable", "Variable",
  299.     DEF_SCALE_VARIABLE, Tk_Offset(Scale, varName), TK_CONFIG_NULL_OK},
  300.     {TK_CONFIG_PIXELS, "-width", "width", "Width",
  301.     DEF_SCALE_WIDTH, Tk_Offset(Scale, width), 0},
  302.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  303.     (char *) NULL, 0, 0}
  304. };
  305.  
  306. /*
  307.  * Forward declarations for procedures defined later in this file:
  308.  */
  309.  
  310. static void        ComputeFormat _ANSI_ARGS_((Scale *scalePtr));
  311. static void        ComputeScaleGeometry _ANSI_ARGS_((Scale *scalePtr));
  312. static int        ConfigureScale _ANSI_ARGS_((Tcl_Interp *interp,
  313.                 Scale *scalePtr, int argc, char **argv,
  314.                 int flags));
  315. static void        DestroyScale _ANSI_ARGS_((ClientData clientData));
  316. static void        DisplayScale _ANSI_ARGS_((ClientData clientData));
  317. static void        DisplayHorizontalScale _ANSI_ARGS_((Scale *scalePtr,
  318.                 Drawable drawable, XRectangle *drawnAreaPtr));
  319. static void        DisplayHorizontalValue _ANSI_ARGS_((Scale *scalePtr,
  320.                 Drawable drawable, double value, int top));
  321. static void        DisplayVerticalScale _ANSI_ARGS_((Scale *scalePtr,
  322.                 Drawable drawable, XRectangle *drawnAreaPtr));
  323. static void        DisplayVerticalValue _ANSI_ARGS_((Scale *scalePtr,
  324.                 Drawable drawable, double value, int rightEdge));
  325. static void        EventuallyRedrawScale _ANSI_ARGS_((Scale *scalePtr,
  326.                 int what));
  327. static double        PixelToValue _ANSI_ARGS_((Scale *scalePtr, int x,
  328.                 int y));
  329. static double        RoundToResolution _ANSI_ARGS_((Scale *scalePtr,
  330.                 double value));
  331. static void        ScaleCmdDeletedProc _ANSI_ARGS_((
  332.                 ClientData clientData));
  333. static int        ScaleElement _ANSI_ARGS_((Scale *scalePtr, int x,
  334.                 int y));
  335. static void        ScaleEventProc _ANSI_ARGS_((ClientData clientData,
  336.                 XEvent *eventPtr));
  337. static char *        ScaleVarProc _ANSI_ARGS_((ClientData clientData,
  338.                 Tcl_Interp *interp, char *name1, char *name2,
  339.                 int flags));
  340. static int        ScaleWidgetCmd _ANSI_ARGS_((ClientData clientData,
  341.                 Tcl_Interp *interp, int argc, char **argv));
  342. static void        SetScaleValue _ANSI_ARGS_((Scale *scalePtr,
  343.                 double value, int setVar, int invokeCommand));
  344. static int        ValueToPixel _ANSI_ARGS_((Scale *scalePtr, double value));
  345.  
  346. /*
  347.  *--------------------------------------------------------------
  348.  *
  349.  * Tk_ScaleCmd --
  350.  *
  351.  *    This procedure is invoked to process the "scale" Tcl
  352.  *    command.  See the user documentation for details on what
  353.  *    it does.
  354.  *
  355.  * Results:
  356.  *    A standard Tcl result.
  357.  *
  358.  * Side effects:
  359.  *    See the user documentation.
  360.  *
  361.  *--------------------------------------------------------------
  362.  */
  363.  
  364. int
  365. Tk_ScaleCmd(clientData, interp, argc, argv)
  366.     ClientData clientData;        /* Main window associated with
  367.                  * interpreter. */
  368.     Tcl_Interp *interp;        /* Current interpreter. */
  369.     int argc;            /* Number of arguments. */
  370.     char **argv;        /* Argument strings. */
  371. {
  372.     Tk_Window tkwin = (Tk_Window) clientData;
  373.     register Scale *scalePtr;
  374.     Tk_Window new;
  375.  
  376.     if (argc < 2) {
  377.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  378.         argv[0], " pathName ?options?\"", (char *) NULL);
  379.     return TCL_ERROR;
  380.     }
  381.  
  382.     new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL);
  383.     if (new == NULL) {
  384.     return TCL_ERROR;
  385.     }
  386.  
  387.     /*
  388.      * Initialize fields that won't be initialized by ConfigureScale,
  389.      * or which ConfigureScale expects to have reasonable values
  390.      * (e.g. resource pointers).
  391.      */
  392.  
  393.     scalePtr = (Scale *) ckalloc(sizeof(Scale));
  394.     scalePtr->tkwin = new;
  395.     scalePtr->display = Tk_Display(new);
  396.     scalePtr->interp = interp;
  397.     scalePtr->widgetCmd = Tcl_CreateCommand(interp,
  398.         Tk_PathName(scalePtr->tkwin), ScaleWidgetCmd,
  399.         (ClientData) scalePtr, ScaleCmdDeletedProc);
  400.     scalePtr->orientUid = NULL;
  401.     scalePtr->vertical = 0;
  402.     scalePtr->width = 0;
  403.     scalePtr->length = 0;
  404.     scalePtr->value = 0;
  405.     scalePtr->varName = NULL;
  406.     scalePtr->fromValue = 0;
  407.     scalePtr->toValue = 0;
  408.     scalePtr->tickInterval = 0;
  409.     scalePtr->resolution = 1;
  410.     scalePtr->bigIncrement = 0.0;
  411.     scalePtr->command = NULL;
  412.     scalePtr->repeatDelay = 0;
  413.     scalePtr->repeatInterval = 0;
  414.     scalePtr->label = NULL;
  415.     scalePtr->labelLength = 0;
  416.     scalePtr->state = tkNormalUid;
  417.     scalePtr->borderWidth = 0;
  418.     scalePtr->bgBorder = NULL;
  419.     scalePtr->activeBorder = NULL;
  420.     scalePtr->troughColorPtr = NULL;
  421.     scalePtr->troughGC = None;
  422.     scalePtr->copyGC = None;
  423.     scalePtr->fontPtr = NULL;
  424.     scalePtr->textColorPtr = NULL;
  425.     scalePtr->textGC = None;
  426.     scalePtr->relief = TK_RELIEF_FLAT;
  427.     scalePtr->highlightWidth = 0;
  428.     scalePtr->highlightBgColorPtr = NULL;
  429.     scalePtr->highlightColorPtr = NULL;
  430.     scalePtr->inset = 0;
  431.     scalePtr->sliderLength = 0;
  432.     scalePtr->showValue = 0;
  433.     scalePtr->horizLabelY = 0;
  434.     scalePtr->horizValueY = 0;
  435.     scalePtr->horizTroughY = 0;
  436.     scalePtr->horizTickY = 0;
  437.     scalePtr->vertTickRightX = 0;
  438.     scalePtr->vertValueRightX = 0;
  439.     scalePtr->vertTroughX = 0;
  440.     scalePtr->vertLabelX = 0;
  441.     scalePtr->cursor = None;
  442.     scalePtr->takeFocus = NULL;
  443.     scalePtr->flags = NEVER_SET;
  444.  
  445.     Tk_SetClass(scalePtr->tkwin, "Scale");
  446.     Tk_CreateEventHandler(scalePtr->tkwin,
  447.         ExposureMask|StructureNotifyMask|FocusChangeMask,
  448.         ScaleEventProc, (ClientData) scalePtr);
  449.     if (ConfigureScale(interp, scalePtr, argc-2, argv+2, 0) != TCL_OK) {
  450.     goto error;
  451.     }
  452.  
  453.     interp->result = Tk_PathName(scalePtr->tkwin);
  454.     return TCL_OK;
  455.  
  456.     error:
  457.     Tk_DestroyWindow(scalePtr->tkwin);
  458.     return TCL_ERROR;
  459. }
  460.  
  461. /*
  462.  *--------------------------------------------------------------
  463.  *
  464.  * ScaleWidgetCmd --
  465.  *
  466.  *    This procedure is invoked to process the Tcl command
  467.  *    that corresponds to a widget managed by this module.
  468.  *    See the user documentation for details on what it does.
  469.  *
  470.  * Results:
  471.  *    A standard Tcl result.
  472.  *
  473.  * Side effects:
  474.  *    See the user documentation.
  475.  *
  476.  *--------------------------------------------------------------
  477.  */
  478.  
  479. static int
  480. ScaleWidgetCmd(clientData, interp, argc, argv)
  481.     ClientData clientData;        /* Information about scale
  482.                      * widget. */
  483.     Tcl_Interp *interp;            /* Current interpreter. */
  484.     int argc;                /* Number of arguments. */
  485.     char **argv;            /* Argument strings. */
  486. {
  487.     register Scale *scalePtr = (Scale *) clientData;
  488.     int result = TCL_OK;
  489.     size_t length;
  490.     int c;
  491.  
  492.     if (argc < 2) {
  493.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  494.         argv[0], " option ?arg arg ...?\"", (char *) NULL);
  495.     return TCL_ERROR;
  496.     }
  497.     Tk_Preserve((ClientData) scalePtr);
  498.     c = argv[1][0];
  499.     length = strlen(argv[1]);
  500.     if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
  501.         && (length >= 2)) {
  502.     if (argc != 3) {
  503.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  504.             argv[0], " cget option\"",
  505.             (char *) NULL);
  506.         goto error;
  507.     }
  508.     result = Tk_ConfigureValue(interp, scalePtr->tkwin, configSpecs,
  509.         (char *) scalePtr, argv[2], 0);
  510.     } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
  511.         && (length >= 3)) {
  512.     if (argc == 2) {
  513.         result = Tk_ConfigureInfo(interp, scalePtr->tkwin, configSpecs,
  514.             (char *) scalePtr, (char *) NULL, 0);
  515.     } else if (argc == 3) {
  516.         result = Tk_ConfigureInfo(interp, scalePtr->tkwin, configSpecs,
  517.             (char *) scalePtr, argv[2], 0);
  518.     } else {
  519.         result = ConfigureScale(interp, scalePtr, argc-2, argv+2,
  520.             TK_CONFIG_ARGV_ONLY);
  521.     }
  522.     } else if ((c == 'c') && (strncmp(argv[1], "coords", length) == 0)
  523.         && (length >= 3)) {
  524.     int x, y ;
  525.     double value;
  526.  
  527.     if ((argc != 2) && (argc != 3)) {
  528.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  529.             argv[0], " coords ?value?\"", (char *) NULL);
  530.         goto error;
  531.     }
  532.     if (argc == 3) {
  533.         if (Tcl_GetDouble(interp, argv[2], &value) != TCL_OK) {
  534.         goto error;
  535.         }
  536.     } else {
  537.         value = scalePtr->value;
  538.     }
  539.     if (scalePtr->vertical) {
  540.         x = scalePtr->vertTroughX + scalePtr->width/2
  541.             + scalePtr->borderWidth;
  542.         y = ValueToPixel(scalePtr, value);
  543.     } else {
  544.         x = ValueToPixel(scalePtr, value);
  545.         y = scalePtr->horizTroughY + scalePtr->width/2
  546.             + scalePtr->borderWidth;
  547.     }
  548.     sprintf(interp->result, "%d %d", x, y);
  549.     } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
  550.     double value;
  551.     int x, y;
  552.  
  553.     if ((argc != 2) && (argc != 4)) {
  554.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  555.             argv[0], " get ?x y?\"", (char *) NULL);
  556.         goto error;
  557.     }
  558.     if (argc == 2) {
  559.         value = scalePtr->value;
  560.     } else {
  561.         if ((Tcl_GetInt(interp, argv[2], &x) != TCL_OK)
  562.             || (Tcl_GetInt(interp, argv[3], &y) != TCL_OK)) {
  563.         goto error;
  564.         }
  565.         value = PixelToValue(scalePtr, x, y);
  566.     }
  567.     sprintf(interp->result, scalePtr->format, value);
  568.     } else if ((c == 'i') && (strncmp(argv[1], "identify", length) == 0)) {
  569.     int x, y, thing;
  570.  
  571.     if (argc != 4) {
  572.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  573.             argv[0], " identify x y\"", (char *) NULL);
  574.         goto error;
  575.     }
  576.     if ((Tcl_GetInt(interp, argv[2], &x) != TCL_OK)
  577.         || (Tcl_GetInt(interp, argv[3], &y) != TCL_OK)) {
  578.         goto error;
  579.     }
  580.     thing = ScaleElement(scalePtr, x,y);
  581.     switch (thing) {
  582.         case TROUGH1:    interp->result = "trough1";    break;
  583.         case SLIDER:    interp->result = "slider";    break;
  584.         case TROUGH2:    interp->result = "trough2";    break;
  585.     }
  586.     } else if ((c == 's') && (strncmp(argv[1], "set", length) == 0)) {
  587.     double value;
  588.  
  589.     if (argc != 3) {
  590.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  591.             argv[0], " set value\"", (char *) NULL);
  592.         goto error;
  593.     }
  594.     if (Tcl_GetDouble(interp, argv[2], &value) != TCL_OK) {
  595.         goto error;
  596.     }
  597.     if (scalePtr->state != tkDisabledUid) {
  598.         SetScaleValue(scalePtr, value, 1, 1);
  599.     }
  600.     } else {
  601.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  602.         "\": must be cget, configure, coords, get, identify, or set",
  603.         (char *) NULL);
  604.     goto error;
  605.     }
  606.     Tk_Release((ClientData) scalePtr);
  607.     return result;
  608.  
  609.     error:
  610.     Tk_Release((ClientData) scalePtr);
  611.     return TCL_ERROR;
  612. }
  613.  
  614. /*
  615.  *----------------------------------------------------------------------
  616.  *
  617.  * DestroyScale --
  618.  *
  619.  *    This procedure is invoked by Tk_EventuallyFree or Tk_Release
  620.  *    to clean up the internal structure of a button at a safe time
  621.  *    (when no-one is using it anymore).
  622.  *
  623.  * Results:
  624.  *    None.
  625.  *
  626.  * Side effects:
  627.  *    Everything associated with the scale is freed up.
  628.  *
  629.  *----------------------------------------------------------------------
  630.  */
  631.  
  632. static void
  633. DestroyScale(clientData)
  634.     ClientData clientData;    /* Info about scale widget. */
  635. {
  636.     register Scale *scalePtr = (Scale *) clientData;
  637.  
  638.     /*
  639.      * Free up all the stuff that requires special handling, then
  640.      * let Tk_FreeOptions handle all the standard option-related
  641.      * stuff.
  642.      */
  643.  
  644.     if (scalePtr->varName != NULL) {
  645.     Tcl_UntraceVar(scalePtr->interp, scalePtr->varName,
  646.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  647.         ScaleVarProc, (ClientData) scalePtr);
  648.     }
  649.     if (scalePtr->troughGC != None) {
  650.     Tk_FreeGC(scalePtr->display, scalePtr->troughGC);
  651.     }
  652.     if (scalePtr->copyGC != None) {
  653.     Tk_FreeGC(scalePtr->display, scalePtr->copyGC);
  654.     }
  655.     if (scalePtr->textGC != None) {
  656.     Tk_FreeGC(scalePtr->display, scalePtr->textGC);
  657.     }
  658.     Tk_FreeOptions(configSpecs, (char *) scalePtr, scalePtr->display, 0);
  659.     ckfree((char *) scalePtr);
  660. }
  661.  
  662. /*
  663.  *----------------------------------------------------------------------
  664.  *
  665.  * ConfigureScale --
  666.  *
  667.  *    This procedure is called to process an argv/argc list, plus
  668.  *    the Tk option database, in order to configure (or
  669.  *    reconfigure) a scale widget.
  670.  *
  671.  * Results:
  672.  *    The return value is a standard Tcl result.  If TCL_ERROR is
  673.  *    returned, then interp->result contains an error message.
  674.  *
  675.  * Side effects:
  676.  *    Configuration information, such as colors, border width,
  677.  *    etc. get set for scalePtr;  old resources get freed,
  678.  *    if there were any.
  679.  *
  680.  *----------------------------------------------------------------------
  681.  */
  682.  
  683. static int
  684. ConfigureScale(interp, scalePtr, argc, argv, flags)
  685.     Tcl_Interp *interp;        /* Used for error reporting. */
  686.     register Scale *scalePtr;    /* Information about widget;  may or may
  687.                  * not already have values for some fields. */
  688.     int argc;            /* Number of valid entries in argv. */
  689.     char **argv;        /* Arguments. */
  690.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  691. {
  692.     XGCValues gcValues;
  693.     GC newGC;
  694.     size_t length;
  695.  
  696.     /*
  697.      * Eliminate any existing trace on a variable monitored by the scale.
  698.      */
  699.  
  700.     if (scalePtr->varName != NULL) {
  701.     Tcl_UntraceVar(interp, scalePtr->varName, 
  702.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  703.         ScaleVarProc, (ClientData) scalePtr);
  704.     }
  705.  
  706.     if (Tk_ConfigureWidget(interp, scalePtr->tkwin, configSpecs,
  707.         argc, argv, (char *) scalePtr, flags) != TCL_OK) {
  708.     return TCL_ERROR;
  709.     }
  710.  
  711.     /*
  712.      * If the scale is tied to the value of a variable, then set up
  713.      * a trace on the variable's value and set the scale's value from
  714.      * the value of the variable, if it exists.
  715.      */
  716.  
  717.     if (scalePtr->varName != NULL) {
  718.     char *stringValue, *end;
  719.     double value;
  720.  
  721.     stringValue = Tcl_GetVar(interp, scalePtr->varName, TCL_GLOBAL_ONLY);
  722.     if (stringValue != NULL) {
  723.         value = strtod(stringValue, &end);
  724.         if ((end != stringValue) && (*end == 0)) {
  725.         scalePtr->value = value;
  726.         }
  727.     }
  728.     Tcl_TraceVar(interp, scalePtr->varName,
  729.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  730.         ScaleVarProc, (ClientData) scalePtr);
  731.     }
  732.  
  733.     /*
  734.      * Several options need special processing, such as parsing the
  735.      * orientation and creating GCs.
  736.      */
  737.  
  738.     length = strlen(scalePtr->orientUid);
  739.     if (strncmp(scalePtr->orientUid, "vertical", length) == 0) {
  740.     scalePtr->vertical = 1;
  741.     } else if (strncmp(scalePtr->orientUid, "horizontal", length) == 0) {
  742.     scalePtr->vertical = 0;
  743.     } else {
  744.     Tcl_AppendResult(interp, "bad orientation \"", scalePtr->orientUid,
  745.         "\": must be vertical or horizontal", (char *) NULL);
  746.     return TCL_ERROR;
  747.     }
  748.  
  749.     scalePtr->fromValue = RoundToResolution(scalePtr, scalePtr->fromValue);
  750.     scalePtr->toValue = RoundToResolution(scalePtr, scalePtr->toValue);
  751.     scalePtr->tickInterval = RoundToResolution(scalePtr,
  752.         scalePtr->tickInterval);
  753.  
  754.     /*
  755.      * Make sure that the tick interval has the right sign so that
  756.      * addition moves from fromValue to toValue.
  757.      */
  758.  
  759.     if ((scalePtr->tickInterval < 0)
  760.         ^ ((scalePtr->toValue - scalePtr->fromValue) <  0)) {
  761.     scalePtr->tickInterval = -scalePtr->tickInterval;
  762.     }
  763.  
  764.     /*
  765.      * Set the scale value to itself;  all this does is to make sure
  766.      * that the scale's value is within the new acceptable range for
  767.      * the scale and reflect the value in the associated variable,
  768.      * if any.
  769.      */
  770.  
  771.     ComputeFormat(scalePtr);
  772.     SetScaleValue(scalePtr, scalePtr->value, 1, 1);
  773.  
  774.     if (scalePtr->label != NULL) {
  775.     scalePtr->labelLength = strlen(scalePtr->label);
  776.     } else {
  777.     scalePtr->labelLength = 0;
  778.     }
  779.  
  780.     if ((scalePtr->state != tkNormalUid)
  781.         && (scalePtr->state != tkDisabledUid)
  782.         && (scalePtr->state != tkActiveUid)) {
  783.     Tcl_AppendResult(interp, "bad state value \"", scalePtr->state,
  784.         "\":  must be normal, active, or disabled", (char *) NULL);
  785.     scalePtr->state = tkNormalUid;
  786.     return TCL_ERROR;
  787.     }
  788.  
  789.     Tk_SetBackgroundFromBorder(scalePtr->tkwin, scalePtr->bgBorder);
  790.  
  791.     gcValues.foreground = scalePtr->troughColorPtr->pixel;
  792.     newGC = Tk_GetGC(scalePtr->tkwin, GCForeground, &gcValues);
  793.     if (scalePtr->troughGC != None) {
  794.     Tk_FreeGC(scalePtr->display, scalePtr->troughGC);
  795.     }
  796.     scalePtr->troughGC = newGC;
  797.     if (scalePtr->copyGC == None) {
  798.     gcValues.graphics_exposures = False;
  799.     scalePtr->copyGC = Tk_GetGC(scalePtr->tkwin, GCGraphicsExposures,
  800.         &gcValues);
  801.     }
  802.     if (scalePtr->highlightWidth < 0) {
  803.     scalePtr->highlightWidth = 0;
  804.     }
  805.     gcValues.font = scalePtr->fontPtr->fid;
  806.     gcValues.foreground = scalePtr->textColorPtr->pixel;
  807.     newGC = Tk_GetGC(scalePtr->tkwin, GCForeground|GCFont, &gcValues);
  808.     if (scalePtr->textGC != None) {
  809.     Tk_FreeGC(scalePtr->display, scalePtr->textGC);
  810.     }
  811.     scalePtr->textGC = newGC;
  812.  
  813.     scalePtr->inset = scalePtr->highlightWidth + scalePtr->borderWidth;
  814.  
  815.     /*
  816.      * Recompute display-related information, and let the geometry
  817.      * manager know how much space is needed now.
  818.      */
  819.  
  820.     ComputeScaleGeometry(scalePtr);
  821.  
  822.     EventuallyRedrawScale(scalePtr, REDRAW_ALL);
  823.     return TCL_OK;
  824. }
  825.  
  826. /*
  827.  *----------------------------------------------------------------------
  828.  *
  829.  * ComputeFormat --
  830.  *
  831.  *    This procedure is invoked to recompute the "format" field
  832.  *    of a scale's widget record, which determines how the value
  833.  *    of the scale is converted to a string.
  834.  *
  835.  * Results:
  836.  *    None.
  837.  *
  838.  * Side effects:
  839.  *    The format field of scalePtr is modified.
  840.  *
  841.  *----------------------------------------------------------------------
  842.  */
  843.  
  844. static void
  845. ComputeFormat(scalePtr)
  846.     Scale *scalePtr;            /* Information about scale widget. */
  847. {
  848.     double maxValue, x;
  849.     int mostSigDigit, numDigits, leastSigDigit, afterDecimal;
  850.     int eDigits, fDigits;
  851.  
  852.     /*
  853.      * Compute the displacement from the decimal of the most significant
  854.      * digit required for any number in the scale's range.
  855.      */
  856.  
  857.     maxValue = fabs(scalePtr->fromValue);
  858.     x = fabs(scalePtr->toValue);
  859.     if (x > maxValue) {
  860.     maxValue = x;
  861.     }
  862.     if (maxValue == 0) {
  863.     maxValue = 1;
  864.     }
  865.     mostSigDigit = floor(log10(maxValue));
  866.  
  867.     /*
  868.      * If the number of significant digits wasn't specified explicitly,
  869.      * compute it. It's the difference between the most significant
  870.      * digit needed to represent any number on the scale and the
  871.      * most significant digit of the smallest difference between
  872.      * numbers on the scale.  In other words, display enough digits so
  873.      * that at least one digit will be different between any two adjacent
  874.      * positions of the scale.
  875.      */
  876.  
  877.     numDigits = scalePtr->digits;
  878.     if (numDigits <= 0) {
  879.     if  (scalePtr->resolution > 0) {
  880.         /*
  881.          * A resolution was specified for the scale, so just use it.
  882.          */
  883.  
  884.         leastSigDigit = floor(log10(scalePtr->resolution));
  885.     } else {
  886.         /*
  887.          * No resolution was specified, so compute the difference
  888.          * in value between adjacent pixels and use it for the least
  889.          * significant digit.
  890.          */
  891.  
  892.         x = fabs(scalePtr->fromValue - scalePtr->toValue);
  893.         if (scalePtr->length > 0) {
  894.         x /= scalePtr->length;
  895.         }
  896.         if (x > 0){
  897.         leastSigDigit = floor(log10(x));
  898.         } else {
  899.         leastSigDigit = 0;
  900.         }
  901.     }
  902.     numDigits = mostSigDigit - leastSigDigit + 1;
  903.     if (numDigits < 1) {
  904.         numDigits = 1;
  905.     }
  906.     }
  907.  
  908.     /*
  909.      * Compute the number of characters required using "e" format and
  910.      * "f" format, and then choose whichever one takes fewer characters.
  911.      */
  912.  
  913.     eDigits = numDigits + 4;
  914.     if (numDigits > 1) {
  915.     eDigits++;            /* Decimal point. */
  916.     }
  917.     afterDecimal = numDigits - mostSigDigit - 1;
  918.     if (afterDecimal < 0) {
  919.     afterDecimal = 0;
  920.     }
  921.     fDigits = (mostSigDigit >= 0) ? mostSigDigit + afterDecimal : afterDecimal;
  922.     if (afterDecimal > 0) {
  923.     fDigits++;            /* Decimal point. */
  924.     }
  925.     if (mostSigDigit < 0) {
  926.     fDigits++;            /* Zero to left of decimal point. */
  927.     }
  928.     if (fDigits <= eDigits) {
  929.     sprintf(scalePtr->format, "%%.%df", afterDecimal);
  930.     } else {
  931.     sprintf(scalePtr->format, "%%.%de", numDigits-1);
  932.     }
  933. }
  934.  
  935. /*
  936.  *----------------------------------------------------------------------
  937.  *
  938.  * ComputeScaleGeometry --
  939.  *
  940.  *    This procedure is called to compute various geometrical
  941.  *    information for a scale, such as where various things get
  942.  *    displayed.  It's called when the window is reconfigured.
  943.  *
  944.  * Results:
  945.  *    None.
  946.  *
  947.  * Side effects:
  948.  *    Display-related numbers get changed in *scalePtr.  The
  949.  *    geometry manager gets told about the window's preferred size.
  950.  *
  951.  *----------------------------------------------------------------------
  952.  */
  953.  
  954. static void
  955. ComputeScaleGeometry(scalePtr)
  956.     register Scale *scalePtr;        /* Information about widget. */
  957. {
  958.     XCharStruct bbox;
  959.     char valueString[PRINT_CHARS];
  960.     int dummy, lineHeight, valuePixels, x, y, extraSpace;
  961.  
  962.     /*
  963.      * Horizontal scales are simpler than vertical ones because
  964.      * all sizes are the same (the height of a line of text);
  965.      * handle them first and then quit.
  966.      */
  967.  
  968.     if (!scalePtr->vertical) {
  969.     lineHeight = scalePtr->fontPtr->ascent + scalePtr->fontPtr->descent;
  970.     y = scalePtr->inset;
  971.     extraSpace = 0;
  972.     if (scalePtr->labelLength != 0) {
  973.         scalePtr->horizLabelY = y + SPACING;
  974.         y += lineHeight + SPACING;
  975.         extraSpace = SPACING;
  976.     }
  977.     if (scalePtr->showValue) {
  978.         scalePtr->horizValueY = y + SPACING;
  979.         y += lineHeight + SPACING;
  980.         extraSpace = SPACING;
  981.     } else {
  982.         scalePtr->horizValueY = y;
  983.     }
  984.     y += extraSpace;
  985.     scalePtr->horizTroughY = y;
  986.     y += scalePtr->width + 2*scalePtr->borderWidth;
  987.     if (scalePtr->tickInterval != 0) {
  988.         scalePtr->horizTickY = y + SPACING;
  989.         y += lineHeight + 2*SPACING;
  990.     }
  991.     Tk_GeometryRequest(scalePtr->tkwin,
  992.         scalePtr->length + 2*scalePtr->inset, y + scalePtr->inset);
  993.     Tk_SetInternalBorder(scalePtr->tkwin, scalePtr->inset);
  994.     return;
  995.     }
  996.  
  997.     /*
  998.      * Vertical scale:  compute the amount of space needed to display
  999.      * the scales value by formatting strings for the two end points;
  1000.      * use whichever length is longer.
  1001.      */
  1002.  
  1003.     sprintf(valueString, scalePtr->format, scalePtr->fromValue);
  1004.     XTextExtents(scalePtr->fontPtr, valueString, (int) strlen(valueString),
  1005.         &dummy, &dummy, &dummy, &bbox);
  1006.     valuePixels = bbox.rbearing - bbox.lbearing;
  1007.     sprintf(valueString, scalePtr->format, scalePtr->toValue);
  1008.     XTextExtents(scalePtr->fontPtr, valueString, (int) strlen(valueString),
  1009.         &dummy, &dummy, &dummy, &bbox);
  1010.     if (valuePixels < bbox.rbearing - bbox.lbearing) {
  1011.     valuePixels = bbox.rbearing - bbox.lbearing;
  1012.     }
  1013.  
  1014.     /*
  1015.      * Assign x-locations to the elements of the scale, working from
  1016.      * left to right.
  1017.      */
  1018.  
  1019.     x = scalePtr->inset;
  1020.     if ((scalePtr->tickInterval != 0) && (scalePtr->showValue)) {
  1021.     scalePtr->vertTickRightX = x + SPACING + valuePixels;
  1022.     scalePtr->vertValueRightX = scalePtr->vertTickRightX + valuePixels
  1023.         + scalePtr->fontPtr->ascent/2;
  1024.     x = scalePtr->vertValueRightX + SPACING;
  1025.     } else if (scalePtr->tickInterval != 0) {
  1026.     scalePtr->vertTickRightX = x + SPACING + valuePixels;
  1027.     scalePtr->vertValueRightX = scalePtr->vertTickRightX;
  1028.     x = scalePtr->vertTickRightX + SPACING;
  1029.     } else if (scalePtr->showValue) {
  1030.     scalePtr->vertTickRightX = x;
  1031.     scalePtr->vertValueRightX = x + SPACING + valuePixels;
  1032.     x = scalePtr->vertValueRightX + SPACING;
  1033.     } else {
  1034.     scalePtr->vertTickRightX = x;
  1035.     scalePtr->vertValueRightX = x;
  1036.     }
  1037.     scalePtr->vertTroughX = x;
  1038.     x += 2*scalePtr->borderWidth + scalePtr->width;
  1039.     if (scalePtr->labelLength == 0) {
  1040.     scalePtr->vertLabelX = 0;
  1041.     } else {
  1042.     XTextExtents(scalePtr->fontPtr, scalePtr->label,
  1043.         scalePtr->labelLength, &dummy, &dummy, &dummy, &bbox);
  1044.     scalePtr->vertLabelX = x + scalePtr->fontPtr->ascent/2 - bbox.lbearing;
  1045.     x = scalePtr->vertLabelX + bbox.rbearing
  1046.         + scalePtr->fontPtr->ascent/2;
  1047.     }
  1048.     Tk_GeometryRequest(scalePtr->tkwin, x + scalePtr->inset,
  1049.         scalePtr->length + 2*scalePtr->inset);
  1050.     Tk_SetInternalBorder(scalePtr->tkwin, scalePtr->inset);
  1051. }
  1052.  
  1053. /*
  1054.  *--------------------------------------------------------------
  1055.  *
  1056.  * DisplayVerticalScale --
  1057.  *
  1058.  *    This procedure redraws the contents of a vertical scale
  1059.  *    window.  It is invoked as a do-when-idle handler, so it only
  1060.  *    runs when there's nothing else for the application to do.
  1061.  *
  1062.  * Results:
  1063.  *    There is no return value.  If only a part of the scale needs
  1064.  *    to be redrawn, then drawnAreaPtr is modified to reflect the
  1065.  *    area that was actually modified.
  1066.  *
  1067.  * Side effects:
  1068.  *    Information appears on the screen.
  1069.  *
  1070.  *--------------------------------------------------------------
  1071.  */
  1072.  
  1073. static void
  1074. DisplayVerticalScale(scalePtr, drawable, drawnAreaPtr)
  1075.     Scale *scalePtr;            /* Widget record for scale. */
  1076.     Drawable drawable;            /* Where to display scale (window
  1077.                      * or pixmap). */
  1078.     XRectangle *drawnAreaPtr;        /* Initally contains area of window;
  1079.                      * if only a part of the scale is
  1080.                      * redrawn, gets modified to reflect
  1081.                      * the part of the window that was
  1082.                      * redrawn. */
  1083. {
  1084.     Tk_Window tkwin = scalePtr->tkwin;
  1085.     int x, y, width, height, shadowWidth, relief;
  1086.     double tickValue;
  1087.     Tk_3DBorder sliderBorder;
  1088.  
  1089.     /*
  1090.      * Display the information from left to right across the window.
  1091.      */
  1092.  
  1093.     if (!(scalePtr->flags & REDRAW_OTHER)) {
  1094.     drawnAreaPtr->x = scalePtr->vertTickRightX;
  1095.     drawnAreaPtr->y = scalePtr->inset;
  1096.     drawnAreaPtr->width = scalePtr->vertTroughX + scalePtr->width
  1097.         + 2*scalePtr->borderWidth - scalePtr->vertTickRightX;
  1098.     drawnAreaPtr->height -= 2*scalePtr->inset;
  1099.     }
  1100.     Tk_Fill3DRectangle(tkwin, drawable, scalePtr->bgBorder,
  1101.         drawnAreaPtr->x, drawnAreaPtr->y, drawnAreaPtr->width,
  1102.         drawnAreaPtr->height, 0, TK_RELIEF_FLAT);
  1103.     if (scalePtr->flags & REDRAW_OTHER) {
  1104.     /*
  1105.      * Display the tick marks.
  1106.      */
  1107.  
  1108.     if (scalePtr->tickInterval != 0) {
  1109.         for (tickValue = scalePtr->fromValue; ;
  1110.             tickValue += scalePtr->tickInterval) {
  1111.         /*
  1112.          * The RoundToResolution call gets rid of accumulated
  1113.          * round-off errors, if any.
  1114.          */
  1115.  
  1116.         tickValue = RoundToResolution(scalePtr, tickValue);
  1117.         if (scalePtr->toValue >= scalePtr->fromValue) {
  1118.             if (tickValue > scalePtr->toValue) {
  1119.             break;
  1120.             }
  1121.         } else {
  1122.             if (tickValue < scalePtr->toValue) {
  1123.             break;
  1124.             }
  1125.         }
  1126.         DisplayVerticalValue(scalePtr, drawable, tickValue,
  1127.             scalePtr->vertTickRightX);
  1128.         }
  1129.     }
  1130.     }
  1131.  
  1132.     /*
  1133.      * Display the value, if it is desired.
  1134.      */
  1135.  
  1136.     if (scalePtr->showValue) {
  1137.     DisplayVerticalValue(scalePtr, drawable, scalePtr->value,
  1138.         scalePtr->vertValueRightX);
  1139.     }
  1140.  
  1141.     /*
  1142.      * Display the trough and the slider.
  1143.      */
  1144.  
  1145.     Tk_Draw3DRectangle(tkwin, drawable,
  1146.         scalePtr->bgBorder, scalePtr->vertTroughX, scalePtr->inset,
  1147.         scalePtr->width + 2*scalePtr->borderWidth,
  1148.         Tk_Height(tkwin) - 2*scalePtr->inset, scalePtr->borderWidth,
  1149.         TK_RELIEF_SUNKEN);
  1150.     XFillRectangle(scalePtr->display, drawable, scalePtr->troughGC,
  1151.         scalePtr->vertTroughX + scalePtr->borderWidth,
  1152.         scalePtr->inset + scalePtr->borderWidth,
  1153.         (unsigned) scalePtr->width,
  1154.         (unsigned) (Tk_Height(tkwin) - 2*scalePtr->inset
  1155.         - 2*scalePtr->borderWidth));
  1156.     if (scalePtr->state == tkActiveUid) {
  1157.     sliderBorder = scalePtr->activeBorder;
  1158.     } else {
  1159.     sliderBorder = scalePtr->bgBorder;
  1160.     }
  1161.     width = scalePtr->width;
  1162.     height = scalePtr->sliderLength/2;
  1163.     x = scalePtr->vertTroughX + scalePtr->borderWidth;
  1164.     y = ValueToPixel(scalePtr, scalePtr->value) - height;
  1165.     shadowWidth = scalePtr->borderWidth/2;
  1166.     if (shadowWidth == 0) {
  1167.     shadowWidth = 1;
  1168.     }
  1169.     relief = (scalePtr->flags & BUTTON_PRESSED) ? TK_RELIEF_SUNKEN
  1170.         : TK_RELIEF_RAISED;
  1171.     Tk_Draw3DRectangle(tkwin, drawable, sliderBorder, x, y, width,
  1172.         2*height, shadowWidth, relief);
  1173.     x += shadowWidth;
  1174.     y += shadowWidth;
  1175.     width -= 2*shadowWidth;
  1176.     height -= shadowWidth;
  1177.     Tk_Fill3DRectangle(tkwin, drawable, sliderBorder, x, y, width,
  1178.         height, shadowWidth, relief);
  1179.     Tk_Fill3DRectangle(tkwin, drawable, sliderBorder, x, y+height,
  1180.         width, height, shadowWidth, relief);
  1181.  
  1182.     /*
  1183.      * Draw the label to the right of the scale.
  1184.      */
  1185.  
  1186.     if ((scalePtr->flags & REDRAW_OTHER) && (scalePtr->labelLength != 0)) {
  1187.     XDrawString(scalePtr->display, drawable,
  1188.         scalePtr->textGC, scalePtr->vertLabelX,
  1189.         scalePtr->inset + (3*scalePtr->fontPtr->ascent)/2,
  1190.         scalePtr->label, scalePtr->labelLength);
  1191.     }
  1192. }
  1193.  
  1194. /*
  1195.  *----------------------------------------------------------------------
  1196.  *
  1197.  * DisplayVerticalValue --
  1198.  *
  1199.  *    This procedure is called to display values (scale readings)
  1200.  *    for vertically-oriented scales.
  1201.  *
  1202.  * Results:
  1203.  *    None.
  1204.  *
  1205.  * Side effects:
  1206.  *    The numerical value corresponding to value is displayed with
  1207.  *    its right edge at "rightEdge", and at a vertical position in
  1208.  *    the scale that corresponds to "value".
  1209.  *
  1210.  *----------------------------------------------------------------------
  1211.  */
  1212.  
  1213. static void
  1214. DisplayVerticalValue(scalePtr, drawable, value, rightEdge)
  1215.     register Scale *scalePtr;    /* Information about widget in which to
  1216.                  * display value. */
  1217.     Drawable drawable;        /* Pixmap or window in which to draw
  1218.                  * the value. */
  1219.     double value;        /* Y-coordinate of number to display,
  1220.                  * specified in application coords, not
  1221.                  * in pixels (we'll compute pixels). */
  1222.     int rightEdge;        /* X-coordinate of right edge of text,
  1223.                  * specified in pixels. */
  1224. {
  1225.     register Tk_Window tkwin = scalePtr->tkwin;
  1226.     int y, dummy, length;
  1227.     char valueString[PRINT_CHARS];
  1228.     XCharStruct bbox;
  1229.  
  1230.     y = ValueToPixel(scalePtr, value) + scalePtr->fontPtr->ascent/2;
  1231.     sprintf(valueString, scalePtr->format, value);
  1232.     length = strlen(valueString);
  1233.     XTextExtents(scalePtr->fontPtr, valueString, length,
  1234.         &dummy, &dummy, &dummy, &bbox);
  1235.  
  1236.     /*
  1237.      * Adjust the y-coordinate if necessary to keep the text entirely
  1238.      * inside the window.
  1239.      */
  1240.  
  1241.     if ((y - bbox.ascent) < (scalePtr->inset + SPACING)) {
  1242.     y = scalePtr->inset + SPACING + bbox.ascent;
  1243.     }
  1244.     if ((y + bbox.descent) > (Tk_Height(tkwin) - scalePtr->inset - SPACING)) {
  1245.     y = Tk_Height(tkwin) - scalePtr->inset - SPACING - bbox.descent;
  1246.     }
  1247.     XDrawString(scalePtr->display, drawable, scalePtr->textGC,
  1248.         rightEdge - bbox.rbearing, y, valueString, length);
  1249. }
  1250.  
  1251. /*
  1252.  *--------------------------------------------------------------
  1253.  *
  1254.  * DisplayHorizontalScale --
  1255.  *
  1256.  *    This procedure redraws the contents of a horizontal scale
  1257.  *    window.  It is invoked as a do-when-idle handler, so it only
  1258.  *    runs when there's nothing else for the application to do.
  1259.  *
  1260.  * Results:
  1261.  *    There is no return value.  If only a part of the scale needs
  1262.  *    to be redrawn, then drawnAreaPtr is modified to reflect the
  1263.  *    area that was actually modified.
  1264.  *
  1265.  * Side effects:
  1266.  *    Information appears on the screen.
  1267.  *
  1268.  *--------------------------------------------------------------
  1269.  */
  1270.  
  1271. static void
  1272. DisplayHorizontalScale(scalePtr, drawable, drawnAreaPtr)
  1273.     Scale *scalePtr;            /* Widget record for scale. */
  1274.     Drawable drawable;            /* Where to display scale (window
  1275.                      * or pixmap). */
  1276.     XRectangle *drawnAreaPtr;        /* Initally contains area of window;
  1277.                      * if only a part of the scale is
  1278.                      * redrawn, gets modified to reflect
  1279.                      * the part of the window that was
  1280.                      * redrawn. */
  1281. {
  1282.     register Tk_Window tkwin = scalePtr->tkwin;
  1283.     int x, y, width, height, shadowWidth, relief;
  1284.     double tickValue;
  1285.     Tk_3DBorder sliderBorder;
  1286.  
  1287.     /*
  1288.      * Display the information from bottom to top across the window.
  1289.      */
  1290.  
  1291.     if (!(scalePtr->flags & REDRAW_OTHER)) {
  1292.     drawnAreaPtr->x = scalePtr->inset;
  1293.     drawnAreaPtr->y = scalePtr->horizValueY;
  1294.     drawnAreaPtr->width -= 2*scalePtr->inset;
  1295.     drawnAreaPtr->height = scalePtr->horizTroughY + scalePtr->width
  1296.         + 2*scalePtr->borderWidth - scalePtr->horizValueY;
  1297.     }
  1298.     Tk_Fill3DRectangle(tkwin, drawable, scalePtr->bgBorder,
  1299.         drawnAreaPtr->x, drawnAreaPtr->y, drawnAreaPtr->width,
  1300.         drawnAreaPtr->height, 0, TK_RELIEF_FLAT);
  1301.     if (scalePtr->flags & REDRAW_OTHER) {
  1302.     /*
  1303.      * Display the tick marks.
  1304.      */
  1305.  
  1306.     if (scalePtr->tickInterval != 0) {
  1307.         for (tickValue = scalePtr->fromValue; ;
  1308.             tickValue += scalePtr->tickInterval) {
  1309.         /*
  1310.          * The RoundToResolution call gets rid of accumulated
  1311.          * round-off errors, if any.
  1312.          */
  1313.  
  1314.         tickValue = RoundToResolution(scalePtr, tickValue);
  1315.         if (scalePtr->toValue >= scalePtr->fromValue) {
  1316.             if (tickValue > scalePtr->toValue) {
  1317.             break;
  1318.             }
  1319.         } else {
  1320.             if (tickValue < scalePtr->toValue) {
  1321.             break;
  1322.             }
  1323.         }
  1324.         DisplayHorizontalValue(scalePtr, drawable, tickValue,
  1325.             scalePtr->horizTickY);
  1326.         }
  1327.     }
  1328.     }
  1329.  
  1330.     /*
  1331.      * Display the value, if it is desired.
  1332.      */
  1333.  
  1334.     if (scalePtr->showValue) {
  1335.     DisplayHorizontalValue(scalePtr, drawable, scalePtr->value,
  1336.         scalePtr->horizValueY);
  1337.     }
  1338.  
  1339.     /*
  1340.      * Display the trough and the slider.
  1341.      */
  1342.  
  1343.     y = scalePtr->horizTroughY;
  1344.     Tk_Draw3DRectangle(tkwin, drawable,
  1345.         scalePtr->bgBorder, scalePtr->inset, y,
  1346.         Tk_Width(tkwin) - 2*scalePtr->inset,
  1347.         scalePtr->width + 2*scalePtr->borderWidth,
  1348.         scalePtr->borderWidth, TK_RELIEF_SUNKEN);
  1349.     XFillRectangle(scalePtr->display, drawable, scalePtr->troughGC,
  1350.         scalePtr->inset + scalePtr->borderWidth,
  1351.         y + scalePtr->borderWidth,
  1352.         (unsigned) (Tk_Width(tkwin) - 2*scalePtr->inset
  1353.         - 2*scalePtr->borderWidth),
  1354.         (unsigned) scalePtr->width);
  1355.     if (scalePtr->state == tkActiveUid) {
  1356.     sliderBorder = scalePtr->activeBorder;
  1357.     } else {
  1358.     sliderBorder = scalePtr->bgBorder;
  1359.     }
  1360.     width = scalePtr->sliderLength/2;
  1361.     height = scalePtr->width;
  1362.     x = ValueToPixel(scalePtr, scalePtr->value) - width;
  1363.     y += scalePtr->borderWidth;
  1364.     shadowWidth = scalePtr->borderWidth/2;
  1365.     if (shadowWidth == 0) {
  1366.     shadowWidth = 1;
  1367.     }
  1368.     relief = (scalePtr->flags & BUTTON_PRESSED) ? TK_RELIEF_SUNKEN
  1369.         : TK_RELIEF_RAISED;
  1370.     Tk_Draw3DRectangle(tkwin, drawable, sliderBorder,
  1371.         x, y, 2*width, height, shadowWidth, relief);
  1372.     x += shadowWidth;
  1373.     y += shadowWidth;
  1374.     width -= shadowWidth;
  1375.     height -= 2*shadowWidth;
  1376.     Tk_Fill3DRectangle(tkwin, drawable, sliderBorder, x, y, width, height,
  1377.         shadowWidth, relief);
  1378.     Tk_Fill3DRectangle(tkwin, drawable, sliderBorder, x+width, y,
  1379.         width, height, shadowWidth, relief);
  1380.  
  1381.     /*
  1382.      * Draw the label at the top of the scale.
  1383.      */
  1384.  
  1385.     if ((scalePtr->flags & REDRAW_OTHER) && (scalePtr->labelLength != 0)) {
  1386.     XDrawString(scalePtr->display, drawable,
  1387.         scalePtr->textGC, scalePtr->inset + scalePtr->fontPtr->ascent/2,
  1388.         scalePtr->horizLabelY + scalePtr->fontPtr->ascent,
  1389.         scalePtr->label, scalePtr->labelLength);
  1390.     }
  1391. }
  1392.  
  1393. /*
  1394.  *----------------------------------------------------------------------
  1395.  *
  1396.  * DisplayHorizontalValue --
  1397.  *
  1398.  *    This procedure is called to display values (scale readings)
  1399.  *    for horizontally-oriented scales.
  1400.  *
  1401.  * Results:
  1402.  *    None.
  1403.  *
  1404.  * Side effects:
  1405.  *    The numerical value corresponding to value is displayed with
  1406.  *    its bottom edge at "bottom", and at a horizontal position in
  1407.  *    the scale that corresponds to "value".
  1408.  *
  1409.  *----------------------------------------------------------------------
  1410.  */
  1411.  
  1412. static void
  1413. DisplayHorizontalValue(scalePtr, drawable, value, top)
  1414.     register Scale *scalePtr;    /* Information about widget in which to
  1415.                  * display value. */
  1416.     Drawable drawable;        /* Pixmap or window in which to draw
  1417.                  * the value. */
  1418.     double value;        /* X-coordinate of number to display,
  1419.                  * specified in application coords, not
  1420.                  * in pixels (we'll compute pixels). */
  1421.     int top;            /* Y-coordinate of top edge of text,
  1422.                  * specified in pixels. */
  1423. {
  1424.     register Tk_Window tkwin = scalePtr->tkwin;
  1425.     int x, y, dummy, length;
  1426.     char valueString[PRINT_CHARS];
  1427.     XCharStruct bbox;
  1428.  
  1429.     x = ValueToPixel(scalePtr, value);
  1430.     y = top + scalePtr->fontPtr->ascent;
  1431.     sprintf(valueString, scalePtr->format, value);
  1432.     length = strlen(valueString);
  1433.     XTextExtents(scalePtr->fontPtr, valueString, length,
  1434.         &dummy, &dummy, &dummy, &bbox);
  1435.  
  1436.     /*
  1437.      * Adjust the x-coordinate if necessary to keep the text entirely
  1438.      * inside the window.
  1439.      */
  1440.  
  1441.     x -= (bbox.rbearing - bbox.lbearing)/2;
  1442.     if ((x + bbox.lbearing) < (scalePtr->inset + SPACING)) {
  1443.     x = scalePtr->inset + SPACING - bbox.lbearing;
  1444.     }
  1445.     if ((x + bbox.rbearing) > (Tk_Width(tkwin) - scalePtr->inset)) {
  1446.     x = Tk_Width(tkwin) - scalePtr->inset - SPACING - bbox.rbearing;
  1447.     }
  1448.     XDrawString(scalePtr->display, drawable, scalePtr->textGC, x, y,
  1449.         valueString, length);
  1450. }
  1451.  
  1452. /*
  1453.  *----------------------------------------------------------------------
  1454.  *
  1455.  * DisplayScale --
  1456.  *
  1457.  *    This procedure is invoked as an idle handler to redisplay
  1458.  *    the contents of a scale widget.
  1459.  *
  1460.  * Results:
  1461.  *    None.
  1462.  *
  1463.  * Side effects:
  1464.  *    The scale gets redisplayed.
  1465.  *
  1466.  *----------------------------------------------------------------------
  1467.  */
  1468.  
  1469. static void
  1470. DisplayScale(clientData)
  1471.     ClientData clientData;    /* Widget record for scale. */
  1472. {
  1473.     Scale *scalePtr = (Scale *) clientData;
  1474.     Tk_Window tkwin = scalePtr->tkwin;
  1475.     Pixmap pixmap;
  1476.     int result;
  1477.     char string[PRINT_CHARS];
  1478.     XRectangle drawnArea;
  1479.  
  1480.     if ((scalePtr->tkwin == NULL) || !Tk_IsMapped(scalePtr->tkwin)) {
  1481.     goto done;
  1482.     }
  1483.  
  1484.     /*
  1485.      * Invoke the scale's command if needed.
  1486.      */
  1487.  
  1488.     Tk_Preserve((ClientData) scalePtr);
  1489.     if ((scalePtr->flags & INVOKE_COMMAND) && (scalePtr->command != NULL)) {
  1490.     sprintf(string, scalePtr->format, scalePtr->value);
  1491.     result = Tcl_VarEval(scalePtr->interp, scalePtr->command,
  1492.         " ", string, (char *) NULL);
  1493.     if (result != TCL_OK) {
  1494.         Tcl_AddErrorInfo(scalePtr->interp,
  1495.             "\n    (command executed by scale)");
  1496.         Tk_BackgroundError(scalePtr->interp);
  1497.     }
  1498.     }
  1499.     scalePtr->flags &= ~INVOKE_COMMAND;
  1500.     if (scalePtr->tkwin == NULL) {
  1501.     Tk_Release((ClientData) scalePtr);
  1502.     return;
  1503.     }
  1504.     Tk_Release((ClientData) scalePtr);
  1505.  
  1506.     /*
  1507.      * In order to avoid screen flashes, this procedure redraws
  1508.      * the scale in a pixmap, then copies the pixmap to the
  1509.      * screen in a single operation.  This means that there's no
  1510.      * point in time where the on-sreen image has been cleared.
  1511.      */
  1512.  
  1513.     pixmap = Tk_GetPixmap(scalePtr->display, Tk_WindowId(tkwin),
  1514.         Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
  1515.     drawnArea.x = 0;
  1516.     drawnArea.y = 0;
  1517.     drawnArea.width = Tk_Width(tkwin);
  1518.     drawnArea.height = Tk_Height(tkwin);
  1519.  
  1520.     /*
  1521.      * Much of the redisplay is done totally differently for
  1522.      * horizontal and vertical scales.  Handle the part that's
  1523.      * different.
  1524.      */
  1525.  
  1526.     if (scalePtr->vertical) {
  1527.     DisplayVerticalScale(scalePtr, pixmap, &drawnArea);
  1528.     } else {
  1529.     DisplayHorizontalScale(scalePtr, pixmap, &drawnArea);
  1530.     }
  1531.  
  1532.     /*
  1533.      * Now handle the part of redisplay that is the same for
  1534.      * horizontal and vertical scales:  border and traversal
  1535.      * highlight.
  1536.      */
  1537.  
  1538.     if (scalePtr->flags & REDRAW_OTHER) {
  1539.     if (scalePtr->relief != TK_RELIEF_FLAT) {
  1540.         Tk_Draw3DRectangle(tkwin, pixmap, scalePtr->bgBorder,
  1541.             scalePtr->highlightWidth, scalePtr->highlightWidth,
  1542.             Tk_Width(tkwin) - 2*scalePtr->highlightWidth,
  1543.             Tk_Height(tkwin) - 2*scalePtr->highlightWidth,
  1544.             scalePtr->borderWidth, scalePtr->relief);
  1545.     }
  1546.     if (scalePtr->highlightWidth != 0) {
  1547.         GC gc;
  1548.     
  1549.         if (scalePtr->flags & GOT_FOCUS) {
  1550.         gc = Tk_GCForColor(scalePtr->highlightColorPtr, pixmap);
  1551.         } else {
  1552.         gc = Tk_GCForColor(scalePtr->highlightBgColorPtr, pixmap);
  1553.         }
  1554.         Tk_DrawFocusHighlight(tkwin, gc, scalePtr->highlightWidth, pixmap);
  1555.     }
  1556.     }
  1557.  
  1558.     /*
  1559.      * Copy the information from the off-screen pixmap onto the screen,
  1560.      * then delete the pixmap.
  1561.      */
  1562.  
  1563.     XCopyArea(scalePtr->display, pixmap, Tk_WindowId(tkwin),
  1564.         scalePtr->copyGC, drawnArea.x, drawnArea.y, drawnArea.width,
  1565.         drawnArea.height, drawnArea.x, drawnArea.y);
  1566.     Tk_FreePixmap(scalePtr->display, pixmap);
  1567.  
  1568.     done:
  1569.     scalePtr->flags &= ~REDRAW_ALL;
  1570. }
  1571.  
  1572. /*
  1573.  *----------------------------------------------------------------------
  1574.  *
  1575.  * ScaleElement --
  1576.  *
  1577.  *    Determine which part of a scale widget lies under a given
  1578.  *    point.
  1579.  *
  1580.  * Results:
  1581.  *    The return value is either TROUGH1, SLIDER, TROUGH2, or
  1582.  *    OTHER, depending on which of the scale's active elements
  1583.  *    (if any) is under the point at (x,y).
  1584.  *
  1585.  * Side effects:
  1586.  *    None.
  1587.  *
  1588.  *----------------------------------------------------------------------
  1589.  */
  1590.  
  1591. static int
  1592. ScaleElement(scalePtr, x, y)
  1593.     Scale *scalePtr;        /* Widget record for scale. */
  1594.     int x, y;            /* Coordinates within scalePtr's window. */
  1595. {
  1596.     int sliderFirst;
  1597.  
  1598.     if (scalePtr->vertical) {
  1599.     if ((x < scalePtr->vertTroughX)
  1600.         || (x >= (scalePtr->vertTroughX + 2*scalePtr->borderWidth +
  1601.         scalePtr->width))) {
  1602.         return OTHER;
  1603.     }
  1604.     if ((y < scalePtr->inset)
  1605.         || (y >= (Tk_Height(scalePtr->tkwin) - scalePtr->inset))) {
  1606.         return OTHER;
  1607.     }
  1608.     sliderFirst = ValueToPixel(scalePtr, scalePtr->value)
  1609.         - scalePtr->sliderLength/2;
  1610.     if (y < sliderFirst) {
  1611.         return TROUGH1;
  1612.     }
  1613.     if (y < (sliderFirst+scalePtr->sliderLength)) {
  1614.         return SLIDER;
  1615.     }
  1616.     return TROUGH2;
  1617.     }
  1618.  
  1619.     if ((y < scalePtr->horizTroughY)
  1620.         || (y >= (scalePtr->horizTroughY + 2*scalePtr->borderWidth +
  1621.         scalePtr->width))) {
  1622.     return OTHER;
  1623.     }
  1624.     if ((x < scalePtr->inset)
  1625.         || (x >= (Tk_Width(scalePtr->tkwin) - scalePtr->inset))) {
  1626.     return OTHER;
  1627.     }
  1628.     sliderFirst = ValueToPixel(scalePtr, scalePtr->value)
  1629.         - scalePtr->sliderLength/2;
  1630.     if (x < sliderFirst) {
  1631.     return TROUGH1;
  1632.     }
  1633.     if (x < (sliderFirst+scalePtr->sliderLength)) {
  1634.     return SLIDER;
  1635.     }
  1636.     return TROUGH2;
  1637. }
  1638.  
  1639. /*
  1640.  *----------------------------------------------------------------------
  1641.  *
  1642.  * PixelToValue --
  1643.  *
  1644.  *    Given a pixel within a scale window, return the scale
  1645.  *    reading corresponding to that pixel.
  1646.  *
  1647.  * Results:
  1648.  *    A double-precision scale reading.  If the value is outside
  1649.  *    the legal range for the scale then it's rounded to the nearest
  1650.  *    end of the scale.
  1651.  *
  1652.  * Side effects:
  1653.  *    None.
  1654.  *
  1655.  *----------------------------------------------------------------------
  1656.  */
  1657.  
  1658. static double
  1659. PixelToValue(scalePtr, x, y)
  1660.     register Scale *scalePtr;        /* Information about widget. */
  1661.     int x, y;                /* Coordinates of point within
  1662.                      * window. */
  1663. {
  1664.     double value, pixelRange;
  1665.  
  1666.     if (scalePtr->vertical) {
  1667.     pixelRange = Tk_Height(scalePtr->tkwin) - scalePtr->sliderLength
  1668.         - 2*scalePtr->inset - 2*scalePtr->borderWidth;
  1669.     value = y;
  1670.     } else {
  1671.     pixelRange = Tk_Width(scalePtr->tkwin) - scalePtr->sliderLength
  1672.         - 2*scalePtr->inset - 2*scalePtr->borderWidth;
  1673.     value = x;
  1674.     }
  1675.  
  1676.     if (pixelRange <= 0) {
  1677.     /*
  1678.      * Not enough room for the slider to actually slide:  just return
  1679.      * the scale's current value.
  1680.      */
  1681.  
  1682.     return scalePtr->value;
  1683.     }
  1684.     value -= scalePtr->sliderLength/2 + scalePtr->inset
  1685.         + scalePtr->borderWidth;
  1686.     value /= pixelRange;
  1687.     if (value < 0) {
  1688.     value = 0;
  1689.     }
  1690.     if (value > 1) {
  1691.     value = 1;
  1692.     }
  1693.     value = scalePtr->fromValue +
  1694.         value * (scalePtr->toValue - scalePtr->fromValue);
  1695.     return RoundToResolution(scalePtr, value);
  1696. }
  1697.  
  1698. /*
  1699.  *----------------------------------------------------------------------
  1700.  *
  1701.  * ValueToPixel --
  1702.  *
  1703.  *    Given a reading of the scale, return the x-coordinate or
  1704.  *    y-coordinate corresponding to that reading, depending on
  1705.  *    whether the scale is vertical or horizontal, respectively.
  1706.  *
  1707.  * Results:
  1708.  *    An integer value giving the pixel location corresponding
  1709.  *    to reading.  The value is restricted to lie within the
  1710.  *    defined range for the scale.
  1711.  *
  1712.  * Side effects:
  1713.  *    None.
  1714.  *
  1715.  *----------------------------------------------------------------------
  1716.  */
  1717.  
  1718. static int
  1719. ValueToPixel(scalePtr, value)
  1720.     register Scale *scalePtr;        /* Information about widget. */
  1721.     double value;            /* Reading of the widget. */
  1722. {
  1723.     int y, pixelRange;
  1724.     double valueRange;
  1725.  
  1726.     valueRange = scalePtr->toValue - scalePtr->fromValue;
  1727.     pixelRange = (scalePtr->vertical ? Tk_Height(scalePtr->tkwin)
  1728.         : Tk_Width(scalePtr->tkwin)) - scalePtr->sliderLength
  1729.         - 2*scalePtr->inset - 2*scalePtr->borderWidth;
  1730.     if (valueRange == 0) {
  1731.     y = 0;
  1732.     } else {
  1733.     y = (int) ((value - scalePtr->fromValue) * pixelRange
  1734.           / valueRange + 0.5);
  1735.     if (y < 0) {
  1736.         y = 0;
  1737.     } else if (y > pixelRange) {
  1738.         y = pixelRange;
  1739.     }
  1740.     }
  1741.     y += scalePtr->sliderLength/2 + scalePtr->inset + scalePtr->borderWidth;
  1742.     return y;
  1743. }
  1744.  
  1745. /*
  1746.  *--------------------------------------------------------------
  1747.  *
  1748.  * ScaleEventProc --
  1749.  *
  1750.  *    This procedure is invoked by the Tk dispatcher for various
  1751.  *    events on scales.
  1752.  *
  1753.  * Results:
  1754.  *    None.
  1755.  *
  1756.  * Side effects:
  1757.  *    When the window gets deleted, internal structures get
  1758.  *    cleaned up.  When it gets exposed, it is redisplayed.
  1759.  *
  1760.  *--------------------------------------------------------------
  1761.  */
  1762.  
  1763. static void
  1764. ScaleEventProc(clientData, eventPtr)
  1765.     ClientData clientData;    /* Information about window. */
  1766.     XEvent *eventPtr;        /* Information about event. */
  1767. {
  1768.     Scale *scalePtr = (Scale *) clientData;
  1769.  
  1770.     if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
  1771.     EventuallyRedrawScale(scalePtr, REDRAW_ALL);
  1772.     } else if (eventPtr->type == DestroyNotify) {
  1773.     if (scalePtr->tkwin != NULL) {
  1774.         scalePtr->tkwin = NULL;
  1775.         Tcl_DeleteCommand(scalePtr->interp,
  1776.             Tcl_GetCommandName(scalePtr->interp, scalePtr->widgetCmd));
  1777.     }
  1778.     if (scalePtr->flags & REDRAW_ALL) {
  1779.         Tk_CancelIdleCall(DisplayScale, (ClientData) scalePtr);
  1780.     }
  1781.     Tk_EventuallyFree((ClientData) scalePtr, DestroyScale);
  1782.     } else if (eventPtr->type == ConfigureNotify) {
  1783.     ComputeScaleGeometry(scalePtr);
  1784.     EventuallyRedrawScale(scalePtr, REDRAW_ALL);
  1785.     } else if (eventPtr->type == FocusIn) {
  1786.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1787.         scalePtr->flags |= GOT_FOCUS;
  1788.         if (scalePtr->highlightWidth > 0) {
  1789.         EventuallyRedrawScale(scalePtr, REDRAW_ALL);
  1790.         }
  1791.     }
  1792.     } else if (eventPtr->type == FocusOut) {
  1793.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1794.         scalePtr->flags &= ~GOT_FOCUS;
  1795.         if (scalePtr->highlightWidth > 0) {
  1796.         EventuallyRedrawScale(scalePtr, REDRAW_ALL);
  1797.         }
  1798.     }
  1799.     }
  1800. }
  1801.  
  1802. /*
  1803.  *----------------------------------------------------------------------
  1804.  *
  1805.  * ScaleCmdDeletedProc --
  1806.  *
  1807.  *    This procedure is invoked when a widget command is deleted.  If
  1808.  *    the widget isn't already in the process of being destroyed,
  1809.  *    this command destroys it.
  1810.  *
  1811.  * Results:
  1812.  *    None.
  1813.  *
  1814.  * Side effects:
  1815.  *    The widget is destroyed.
  1816.  *
  1817.  *----------------------------------------------------------------------
  1818.  */
  1819.  
  1820. static void
  1821. ScaleCmdDeletedProc(clientData)
  1822.     ClientData clientData;    /* Pointer to widget record for widget. */
  1823. {
  1824.     Scale *scalePtr = (Scale *) clientData;
  1825.     Tk_Window tkwin = scalePtr->tkwin;
  1826.  
  1827.     /*
  1828.      * This procedure could be invoked either because the window was
  1829.      * destroyed and the command was then deleted (in which case tkwin
  1830.      * is NULL) or because the command was deleted, and then this procedure
  1831.      * destroys the widget.
  1832.      */
  1833.  
  1834.     if (tkwin != NULL) {
  1835.     scalePtr->tkwin = NULL;
  1836.     Tk_DestroyWindow(tkwin);
  1837.     }
  1838. }
  1839.  
  1840. /*
  1841.  *--------------------------------------------------------------
  1842.  *
  1843.  * SetScaleValue --
  1844.  *
  1845.  *    This procedure changes the value of a scale and invokes
  1846.  *    a Tcl command to reflect the current position of a scale
  1847.  *
  1848.  * Results:
  1849.  *    None.
  1850.  *
  1851.  * Side effects:
  1852.  *    A Tcl command is invoked, and an additional error-processing
  1853.  *    command may also be invoked.  The scale's slider is redrawn.
  1854.  *
  1855.  *--------------------------------------------------------------
  1856.  */
  1857.  
  1858. static void
  1859. SetScaleValue(scalePtr, value, setVar, invokeCommand)
  1860.     register Scale *scalePtr;    /* Info about widget. */
  1861.     double value;        /* New value for scale.  Gets adjusted
  1862.                  * if it's off the scale. */
  1863.     int setVar;            /* Non-zero means reflect new value through
  1864.                  * to associated variable, if any. */
  1865.     int invokeCommand;        /* Non-zero means invoked -command option
  1866.                  * to notify of new value, 0 means don't. */
  1867. {
  1868.     char string[PRINT_CHARS];
  1869.  
  1870.     value = RoundToResolution(scalePtr, value);
  1871.     if ((value < scalePtr->fromValue)
  1872.         ^ (scalePtr->toValue < scalePtr->fromValue)) {
  1873.     value = scalePtr->fromValue;
  1874.     }
  1875.     if ((value > scalePtr->toValue)
  1876.         ^ (scalePtr->toValue < scalePtr->fromValue)) {
  1877.     value = scalePtr->toValue;
  1878.     }
  1879.     if (scalePtr->flags & NEVER_SET) {
  1880.     scalePtr->flags &= ~NEVER_SET;
  1881.     } else if (scalePtr->value == value) {
  1882.     return;
  1883.     }
  1884.     scalePtr->value = value;
  1885.     if (invokeCommand) {
  1886.     scalePtr->flags |= INVOKE_COMMAND;
  1887.     }
  1888.     EventuallyRedrawScale(scalePtr, REDRAW_SLIDER);
  1889.  
  1890.     if (setVar && (scalePtr->varName != NULL)) {
  1891.     sprintf(string, scalePtr->format, scalePtr->value);
  1892.     scalePtr->flags |= SETTING_VAR;
  1893.     Tcl_SetVar(scalePtr->interp, scalePtr->varName, string,
  1894.            TCL_GLOBAL_ONLY);
  1895.     scalePtr->flags &= ~SETTING_VAR;
  1896.     }
  1897. }
  1898.  
  1899. /*
  1900.  *--------------------------------------------------------------
  1901.  *
  1902.  * EventuallyRedrawScale --
  1903.  *
  1904.  *    Arrange for part or all of a scale widget to redrawn at
  1905.  *    the next convenient time in the future.
  1906.  *
  1907.  * Results:
  1908.  *    None.
  1909.  *
  1910.  * Side effects:
  1911.  *    If "what" is REDRAW_SLIDER then just the slider and the
  1912.  *    value readout will be redrawn;  if "what" is REDRAW_ALL
  1913.  *    then the entire widget will be redrawn.
  1914.  *
  1915.  *--------------------------------------------------------------
  1916.  */
  1917.  
  1918. static void
  1919. EventuallyRedrawScale(scalePtr, what)
  1920.     register Scale *scalePtr;    /* Information about widget. */
  1921.     int what;            /* What to redraw:  REDRAW_SLIDER
  1922.                  * or REDRAW_ALL. */
  1923. {
  1924.     if ((what == 0) || (scalePtr->tkwin == NULL)
  1925.         || !Tk_IsMapped(scalePtr->tkwin)) {
  1926.     return;
  1927.     }
  1928.     if ((scalePtr->flags & REDRAW_ALL) == 0) {
  1929.     Tk_DoWhenIdle(DisplayScale, (ClientData) scalePtr);
  1930.     }
  1931.     scalePtr->flags |= what;
  1932. }
  1933.  
  1934. /*
  1935.  *--------------------------------------------------------------
  1936.  *
  1937.  * RoundToResolution --
  1938.  *
  1939.  *    Round a given floating-point value to the nearest multiple
  1940.  *    of the scale's resolution.
  1941.  *
  1942.  * Results:
  1943.  *    The return value is the rounded result.
  1944.  *
  1945.  * Side effects:
  1946.  *    None.
  1947.  *
  1948.  *--------------------------------------------------------------
  1949.  */
  1950.  
  1951. static double
  1952. RoundToResolution(scalePtr, value)
  1953.     Scale *scalePtr;        /* Information about scale widget. */
  1954.     double value;        /* Value to round. */
  1955. {
  1956.     double rem;
  1957.  
  1958.     if (scalePtr->resolution <= 0) {
  1959.     return value;
  1960.     }
  1961.     rem = fmod(value, scalePtr->resolution);
  1962.     if (rem < 0) {
  1963.     rem = scalePtr->resolution + rem;
  1964.     }
  1965.     value -= rem;
  1966.     if (rem >= scalePtr->resolution/2) {
  1967.     value += scalePtr->resolution;
  1968.     }
  1969.     return value;
  1970. }
  1971.  
  1972. /*
  1973.  *----------------------------------------------------------------------
  1974.  *
  1975.  * ScaleVarProc --
  1976.  *
  1977.  *    This procedure is invoked by Tcl whenever someone modifies a
  1978.  *    variable associated with a scale widget.
  1979.  *
  1980.  * Results:
  1981.  *    NULL is always returned.
  1982.  *
  1983.  * Side effects:
  1984.  *    The value displayed in the scale will change to match the
  1985.  *    variable's new value.  If the variable has a bogus value then
  1986.  *    it is reset to the value of the scale.
  1987.  *
  1988.  *----------------------------------------------------------------------
  1989.  */
  1990.  
  1991.     /* ARGSUSED */
  1992. static char *
  1993. ScaleVarProc(clientData, interp, name1, name2, flags)
  1994.     ClientData clientData;    /* Information about button. */
  1995.     Tcl_Interp *interp;        /* Interpreter containing variable. */
  1996.     char *name1;        /* Name of variable. */
  1997.     char *name2;        /* Second part of variable name. */
  1998.     int flags;            /* Information about what happened. */
  1999. {
  2000.     register Scale *scalePtr = (Scale *) clientData;
  2001.     char *stringValue, *end, *result;
  2002.     double value;
  2003.  
  2004.     /*
  2005.      * If the variable is unset, then immediately recreate it unless
  2006.      * the whole interpreter is going away.
  2007.      */
  2008.  
  2009.     if (flags & TCL_TRACE_UNSETS) {
  2010.     if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
  2011.         Tcl_TraceVar(interp, scalePtr->varName,
  2012.             TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  2013.             ScaleVarProc, clientData);
  2014.         scalePtr->flags |= NEVER_SET;
  2015.         SetScaleValue(scalePtr, scalePtr->value, 1, 0);
  2016.     }
  2017.     return (char *) NULL;
  2018.     }
  2019.  
  2020.     /*
  2021.      * If we came here because we updated the variable (in SetScaleValue),
  2022.      * then ignore the trace.  Otherwise update the scale with the value
  2023.      * of the variable.
  2024.      */
  2025.  
  2026.     if (scalePtr->flags & SETTING_VAR) {
  2027.     return (char *) NULL;
  2028.     }
  2029.     result = NULL;
  2030.     stringValue = Tcl_GetVar(interp, scalePtr->varName, TCL_GLOBAL_ONLY);
  2031.     if (stringValue != NULL) {
  2032.     value = strtod(stringValue, &end);
  2033.     if ((end == stringValue) || (*end != 0)) {
  2034.         result = "can't assign non-numeric value to scale variable";
  2035.     } else {
  2036.         scalePtr->value = value;
  2037.     }
  2038.  
  2039.     /*
  2040.      * This code is a bit tricky because it sets the scale's value before
  2041.      * calling SetScaleValue.  This way, SetScaleValue won't bother to
  2042.      * set the variable again or to invoke the -command.  However, it
  2043.      * also won't redisplay the scale, so we have to ask for that
  2044.      * explicitly.
  2045.      */
  2046.  
  2047.     SetScaleValue(scalePtr, scalePtr->value, 1, 0);
  2048.     EventuallyRedrawScale(scalePtr, REDRAW_SLIDER);
  2049.     }
  2050.  
  2051.     return result;
  2052. }
  2053.